package com.coolingme.demo.controller;

import com.coolingme.demo.service.DemoService;
import com.coolingme.springframework.web.annotation.MeAutowired;
import com.coolingme.springframework.web.annotation.MeController;
import com.coolingme.springframework.web.annotation.MeRequestMapping;
import com.coolingme.springframework.web.annotation.MeRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 控制层
 *
 * @author wangyue
 * @date 2020/10/23 15:14
 */
@MeController
@MeRequestMapping("/demo")
public class DemoController {

//    @MeAutowired // 根据类型注入
//    @MeAutowired("demoServiceImpl") // 根据名称注入，service实现类名首字母小写
    @MeAutowired("demoService") // 根据名称注入，使用service的别名
    private DemoService demoService;

    @MeRequestMapping("/index")
    public void hello() {
        System.out.println("Hello,Spring");
    }

    @MeRequestMapping("/hello")
    public void hello(HttpServletRequest req, HttpServletResponse resp,
                      @MeRequestParam("name") String name) {
        try {
            String result = demoService.hello(name);
            System.out.println(result);
            resp.getWriter().write(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @MeRequestMapping("/add")
    public void add(HttpServletResponse resp,
                    @MeRequestParam("a") String a, @MeRequestParam("b") String b) {
        try {
            resp.getWriter().write(a + "+" + b + "=" + (Integer.valueOf(a) + Integer.valueOf(b)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @MeRequestMapping("/remove")
    public void remove(@MeRequestParam("id") String id, HttpServletResponse resp) {
        try {
            resp.getWriter().write("remove id:" + id + " success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
